function [s] = alpha_edit(cmd);
% alpha_edit:
% Usage: alpha_edit(cmd)
%  called internally with 'edit stim2', when the stim type is an alpha waveform.
% Creates a GUI to adjust the stimulus parameters for ALPHA waveforms
%  only. 
%  8/01 Paul B. Manis pmanis@med.unc.edu
%  first version: rough, but works.

global SCOPE_FLAG STIM

persistent STIM2
persistent MOUSE_STAT % 0 is up, 1 is down, 2 is moving...
persistent sign;

bkcolor = [8 8 8]/10;

if(nargin == 0)
   
   STIM2 = g; % get a secondary stimulus parameter block
   if(isempty(STIM2)) % ? no block
      return;
   end;
   if(~strcmp(STIM2.Method.v, 'alpha')) % the method must be pulse
      QueMessage('alph_aedit: Requires stimulus method ALPHA', 1);
      return;
   end;
   
   % method is alpha, so we can go ahead and make a new window for the controls
   
   h = findobj('Tag', 'alpha_edit');
   if(isempty(h))
      h = figure('Units', 'Normalized', ...
         'Number', 'off', ...
         'Name', 'Pulse Editor', ...
         'MenuBar', 'none', ...
         'Color', bkcolor, ...
         'Position', [0.25, 0.25, 0.5, 0.5], ...
         'WindowButtonDownFcn', 'alpha_edit(''mouse_down'')', ...
         'WindowButtonUpFcn', 'alpha_edit(''mouse_up'')', ...
         'WindowButtonMotionFcn', 'alpha_edit(''mouse_move'')', ...
         'Tag', 'alpha_edit');
      MOUSE_STAT = 0;
   else
      figure(h);
   end;
   clf(h);
   
   set(h, 'Name', sprintf('Alpha Editor: %s', STIM2.Name.v));
   if(~strmatch(STIM2.Method.v, 'alpha', 'exact'))
      QueMessage('alpha_edit requires ALPHA stimulus type', 1);
      return;
   end;
   
   
   % preview display on top of window for reference.
   hp = subplot('Position', [0.05 0.8 0.90 0.12]);
   set(hp, 'color', [0 0 0], ...
      'Tag', 'stimpreview', 'Parent', h);
   plot(hp, 'Xdata', [0 1], 'YData', [NaN NaN]);
   top = 0.8;
   dy=0.05;
   %slider for amplitude
   
   if(STIM2.Amplitude.v < 0)
      slamp = -STIM2.Amplitude.v;
      sign = -1;
   else
      slamp = STIM2.Amplitude.v;
      sign = 1;
   end;
   maxamp = 1000;
   hs = uicontrol('Parent', h, ...
      'Units', 'Normalized', ...
      'Position', [0.4, 0.05, 0.04, 0.5], ...
      'BackgroundColor', bkcolor, ...
      'Style', 'slider', ...
      'String', 'LEVEL', ...
      'min', 0, ...
      'max', maxamp, ...
      'sliderstep', [.1 10]/maxamp, ...
      'value', slamp, ... 
      'callback', 'alpha_edit(''updateslider'')', ...
      'Tag', 'stim_slider');
   
   v = get(hs, 'Value');
   hp = uicontrol('Parent', h, ...
      'Units', 'Normalized', ...
      'Position', [0.36 0.56 0.15 0.04], ...
      'BackgroundColor', bkcolor, ...
      'Style', 'text', ...
      'String', sprintf('%7.2f', v*sign), ...
      'HorizontalAlignment',  'center', ...
      'Tag', 'stim_value');
   
   ha = axes('Parent', h, ...
      'Units', 'Normalized', ...
      'Position', [0.395, 0.05+(0.5*0.1), 0.02, 0.5 - (0.5*0.2)]);
   set(ha, 'Color', bkcolor, 'XColor', bkcolor);
   set(ha, 'YAxisLocation', 'left', 'YLim', [0 maxamp]);
   set(ha, 'YTick',  [0 100 200 300 400 500 600 700 800 900 1000]);
   set(ha, 'TickDir', 'in');
   uicontrol('Parent', h, 'Units', 'Normalized', 'Position', [0.36 0.6, 0.15, 0.04], ...
      'BackgroundColor', bkcolor, ...
      'Style', 'text', 'String', 'Amplitude', 'FontSize', 10, 'FontWeight', 'Bold', 'FontName', 'Arial', ...
      'HorizontalAlignment',  'center');
   
   %Buttons for amplitude sign..
   uicontrol('Parent', h', 'units', 'normalized', 'Position', [0.47 (top - 7*dy) 0.05 0.05], ...
      'BackgroundColor', bkcolor, ...
      'Style', 'pushbutton', 'string', '+', ...
      'fontsize', 12, ...
      'Callback', 'alpha_edit(''AmpPlus'')', ...
      'Tag', 'stmctl_btn_positive');
	uicontrol('Parent', h', 'units', 'normalized', 'Position', [0.47 (top - 8.25*dy) 0.05 0.05], ...
      'BackgroundColor', bkcolor, ...
      'Style', 'pushbutton', 'string', '-', ...
      'fontsize', 12, ...
      'Callback', 'alpha_edit(''AmpMinus'')', ...
      'Tag', 'stmctl_btn_negative');


   %--------------------
   % Slider for Number of pulses
   maxnp = 100;
   hs = uicontrol('Parent', h, ...
      'Units', 'Normalized', ...
      'Position', [0.05, 0.05, 0.035, 0.5], ...
      'BackgroundColor', bkcolor, ...
      'Style', 'slider', ...
      'String', 'LEVEL', ...
      'min', 1, ...
      'max', maxnp, ...
      'sliderstep', [1 10]/maxnp, ...
      'value', STIM2.Npulses.v, ... 
      'callback', 'alpha_edit(''np'')', ...
      'Tag', 'stim_ctl_np');
   
   v = get(hs, 'Value');
   hp = uicontrol('Parent', h, ...
      'Units', 'Normalized', ...
      'Position', [0.035 0.56 0.05 0.04], ...
      'BackgroundColor', bkcolor, ...
      'Style', 'text', ...
      'String', sprintf('%4d', v), ...
      'Tag', 'stim_np_val');
   
   
   uicontrol('Parent', h, 'Units', 'Normalized', 'Position', [0.035 0.6, 0.05, 0.04], ...
      'BackgroundColor', bkcolor, ...
      'Style', 'text', 'String', '#', 'FontSize', 8, 'FontWeight', 'Bold', 'FontName', 'Arial');
   
   %--------------------
   % Slider for InterPulse Interval
   maxipi = 1000;
   hs = uicontrol('Parent', h, ...
      'Units', 'Normalized', ...
      'Position', [0.12, 0.05, 0.04, 0.5], ...
      'BackgroundColor', bkcolor, ...
      'Style', 'slider', ...
      'String', 'IPI', ...
      'min', 1, ...
      'max', maxipi, ...
      'sliderstep', [1 10]/maxipi, ...
      'value', STIM2.IPI.v, ... 
      'callback', 'alpha_edit(''ipi'')', ...
      'Tag', 'stim_ctl_ipi');
   
   v = get(hs, 'Value');
   hp = uicontrol('Parent', h, ...
      'Units', 'Normalized', ...
      'Position', [0.11 0.56 0.06 0.04], ...
      'BackgroundColor', bkcolor, ...
      'Style', 'text', ...
      'String', sprintf('%6.1f', v), ...
      'Tag', 'stim_ipi_val');
   
   uicontrol('Parent', h, 'Units', 'Normalized', 'Position', [0.11 0.6, 0.06, 0.04], ...
      'BackgroundColor', bkcolor, ...
      'Style', 'text', 'String', 'IPI', 'FontSize', 8, 'FontWeight', 'Bold', 'FontName', 'Arial');
   
   %--------------------
   % Slider for Delay to first pulse
   maxdelay = 1000;
   hs = uicontrol('Parent', h, ...
      'Units', 'Normalized', ...
      'Position', [0.19, 0.05, 0.04, 0.5], ...
      'Style', 'slider', ...
      'BackgroundColor', bkcolor, ...
      'String', 'Delay', ...
      'min', 1, ...
      'max', maxdelay, ...
      'sliderstep', [1 10]/maxdelay, ...
      'value', STIM2.Delay.v, ... 
      'callback', 'alpha_edit(''delay'')', ...
      'Tag', 'stim_ctl_delay');
   
   v = get(hs, 'Value');
   hp = uicontrol('Parent', h, ...
      'Units', 'Normalized', ...
      'Position', [0.18 0.56 0.06 0.04], ...
      'BackgroundColor', bkcolor, ...
      'Style', 'text', ...
      'String', sprintf('%6.1f', v), ...
      'Tag', 'stim_delay_val');
   
   uicontrol('Parent', h, 'Units', 'Normalized', 'Position', [0.18 0.6, 0.06, 0.04], ...
      'BackgroundColor', bkcolor, ...
      'Style', 'text', 'String', 'Delay', 'FontSize', 8, 'FontWeight', 'Bold', 'FontName', 'Arial');
   
   %--------------------
   % Slider for Alpha parameter 
   maxalpha = 10;
   hs = uicontrol('Parent', h, ...
      'Units', 'Normalized', ...
      'Position', [0.26, 0.05, 0.04, 0.5], ...
      'BackgroundColor', bkcolor, ...
      'Style', 'slider', ...
      'String', 'Alpha', ...
      'min', 0.01, ...
      'max', maxalpha, ...
      'sliderstep', [0.01 1]/maxalpha, ...
      'value', STIM2.Alpha.v, ... 
      'callback', 'alpha_edit(''alpha1'')', ...
      'Tag', 'stim_ctl_alpha1');
   
   v = get(hs, 'Value');
   hp = uicontrol('Parent', h, ...
      'Units', 'Normalized', ...
      'Position', [0.25 0.56 0.06 0.04], ...
      'BackgroundColor', bkcolor, ...
      'Style', 'text', ...
      'String', sprintf('%7.2f', v), ...
      'Tag', 'stim_alpha1_val');
   
   uicontrol('Parent', h, 'Units', 'Normalized', 'Position', [0.25 0.6, 0.06, 0.04], ...
      'BackgroundColor', bkcolor, ...
      'Style', 'text', 'String', 'Alpha', 'FontSize', 8, 'FontWeight', 'Bold', 'FontName', 'Arial');
   
   
   
   %-------------------Sequence information: type, sequence, and position
   top = 0.6; dy = 0.07;
   % ----- selection of parameters.
   uicontrol('Parent', h, 'Units', 'Normalized', 'Position', [0.55 (top - 0 * dy) 0.15 0.05], ...
      'HorizontalAlignment', 'right', ...
      'BackgroundColor', bkcolor, ...
      'Style', 'text', 'String', 'SeqPars');
   uicontrol('Parent', h, 'units', 'normalized', 'Position', [0.72 (top - 0 * dy) 0.25 0.05], ...
      'HorizontalAlignment', 'right', ...
      'BackgroundColor', bkcolor, ...
      'Style', 'edit', 'String', sprintf('%s', STIM2.SeqParList.v), ...
      'Callback', 'alpha_edit(''seq_parlist'')', ...
      'Tag', 'stmctl_seqparlisit');
   
   uicontrol('Parent', h, 'Units', 'Normalized', 'Position', [0.55 (top - 1 * dy) 0.15 0.05], ...
      'HorizontalAlignment', 'right', ...
      'BackgroundColor', bkcolor, ...
      'Style', 'text', 'String', 'SeqStepNo');
   uicontrol('Parent', h, 'units', 'normalized', 'Position', [0.72 (top - 1 * dy) 0.25 0.05], ...
      'HorizontalAlignment', 'right', ...
      'BackgroundColor', bkcolor, ...
      'Style', 'edit', 'String', sprintf('%8d', STIM2.SeqLevelList.v), ...
      'Callback', 'alpha_edit(''seq_no'')', ...
      'Tag', 'stmctl_seqsno');
   
   uicontrol('Parent', h, 'Units', 'Normalized', 'Position', [0.55 (top - 2 * dy) 0.15 0.05], ...
      'HorizontalAlignment', 'right', ...
      'BackgroundColor', bkcolor, ...
      'Style', 'text', 'String', 'Sequence');
   uicontrol('Parent', h, 'units', 'normalized', 'Position', [0.72 (top - 2 * dy) 0.25 0.05], ...
      'HorizontalAlignment', 'right', ...
      'BackgroundColor', bkcolor, ...
      'Style', 'edit', 'String', sprintf('%s', STIM2.Sequence.v), ...
      'Callback', 'alpha_edit(''seq_sequence'')', ...
      'Tag', 'stmctl_sequence');
   
  
   % command buttons
   
   uicontrol('Parent', h', 'units', 'normalized', 'Position', [0.72 (top - 3*dy) 0.12 0.06], ...
      'BackgroundColor', bkcolor, ...
      'Style', 'pushbutton', 'string', 'Open', ...
      'Callback', 'alpha_edit(''open'')', ...
      'Tag', 'stmctl_btn_open');
   
   uicontrol('Parent', h', 'units', 'normalized', 'Position', [0.85 (top - 3*dy) 0.12 0.06], ...
      'BackgroundColor', bkcolor, ...
      'Style', 'pushbutton', 'string', 'Save', ...
      'Callback', 'alpha_edit(''save'')', ...
      'Tag', 'stmctl_btn_save');
   
   uicontrol('Parent', h', 'units', 'normalized', 'Position', [0.85 (top - 4*dy) 0.12 0.06], ...
      'BackgroundColor', bkcolor, ...
      'Style', 'pushbutton', 'string', 'Close', ...
      'Callback', 'alpha_edit(''close'')', ...
      'Tag', 'stmctl_btn_close');   
   
   uicontrol('Parent', h', 'units', 'normalized', 'Position', [0.72 (top - 5*dy) 0.25 0.06], ...
      'BackgroundColor', bkcolor, ...
      'Style', 'pushbutton', 'string', 'Load (DAC0)', ...
      'Callback', 'alpha_edit(''load'')', ...
      'Tag', 'stmctl_btn_load');
   
   uicontrol('Parent', h', 'units', 'normalized', 'Position', [0.72 (top - 6*dy) 0.25 0.06], ...
      'BackgroundColor', bkcolor, ...
      'Style', 'pushbutton', 'string', 'Addchan (DAC1)', ...
      'Callback', 'alpha_edit(''addch'')', ...
      'Tag', 'stmctl_btn_addch');
   
   uicontrol('Parent', h', 'units', 'normalized', 'Position', [0.72 (top - 7*dy) 0.25 0.06], ...
      'BackgroundColor', bkcolor, ...
      'Style', 'pushbutton', 'string', 'Superimpose (DAC0)', ...
      'Callback', 'alpha_edit(''superimpose'')', ...
      'Tag', 'stmctl_btn_superimpose');


   
   stim_pv(STIM2);
   
else
   switch(cmd)
      case 'open'
      STIM2 = g2;
      stim_pv(STIM2);
      return;
      
   case 'updateslider'
      hp = findobj('Tag', 'stim_value');
      hs = findobj('Tag', 'stim_slider');
      newlevel = sign*get(hs, 'Value');
      set(hp, 'String', sprintf('%7.2f', newlevel));
      STIM2.Amplitude.v = newlevel;
      if(~exist('MOUSE_STAT') | MOUSE_STAT == 0)
         stim_pv(STIM2);
      end;
      return;

      
   case 'np'
      hcmd = findobj('Tag', 'stim_ctl_np');
      oldnp = STIM2.Npulses.v;
      STIM2.Npulses.v = floor(get(hcmd, 'Value'));
      hnp = findobj('Tag', 'stim_np_val');
      set(hnp, 'String', sprintf('%4d', STIM2.Npulses.v));
      if(oldnp ~= STIM2.Npulses.v)
         stim_pv(STIM2);
      end;
      
   case 'ipi'
      hcmd = findobj('Tag', 'stim_ctl_ipi');
      oldipi = STIM2.IPI.v;
      STIM2.IPI.v = get(hcmd, 'Value');
      hnp = findobj('Tag', 'stim_ipi_val');
      set(hnp, 'String', sprintf('%6.1f', STIM2.IPI.v));
      if(oldipi ~= STIM2.IPI.v)
         stim_pv(STIM2);
      end;
      
   case 'delay'
      hcmd = findobj('Tag', 'stim_ctl_delay');
      olddelay = STIM2.Delay.v;
      STIM2.Delay.v = get(hcmd, 'Value');
      hnp = findobj('Tag', 'stim_delay_val');
      set(hnp, 'String', sprintf('%6.1f', STIM2.Delay.v));
      if(olddelay ~= STIM2.Delay.v)
         stim_pv(STIM2);
      end;
      
   case 'alpha1'
      hcmd = findobj('Tag', 'stim_ctl_alpha1');
      oldalpha1 = STIM2.Alpha.v;
      STIM2.Alpha.v = get(hcmd, 'Value');
      hnp = findobj('Tag', 'stim_alpha1_val');
      set(hnp, 'String', sprintf('%7.2f', STIM2.Alpha.v));
      if(oldalpha1 ~= STIM2.Alpha.v)
         stim_pv(STIM2);
      end;
      
      
   case 'lev'
      hcmd = findobj('Tag', 'stmctl_lev');
      lev = get(hcmd, 'String');
      oldlev = STIM2.Level.v(1);
      STIM2.Level.v(1) = lev;
      STIM2.Level.v(2) = -lev;
      set(hcmd, 'String', sprintf('%6.2f', STIM2.Level.v(1)));
      if(oldlev ~= STIM2.Level.v(1))
         stim_pv(STIM2);
      end;
      
   case 'seq_parlist'
      hcmd = findobj('Tag', 'stmctl_seqpars');
      sno = get(hcmd, 'String');
      oldsno = STIM2.SeqStepList.v;
      STIM2.SeqStepList.v = sno;
      set(hcmd, 'String', sprintf('%s', STIM2.SeqStepList.v));
      if(~strcmp(oldsno,STIM2.SeqStepList.v))
         stim_pv(STIM2);
      end;
      
   case 'seq_no'
      hcmd = findobj('Tag', 'stmctl_seqsno');
      sno = get(hcmd, 'String');
      oldsno = STIM2.SeqStepList.v;
      STIM2.SeqStepList.v = str2num(sn{1});
      if(length(STIM2.SeqStepList.v > 1))
         STIM2.SeqStepList.v = STIM2.SeqStepList.v(1);
      end;
      set(hcmd, 'String', sprintf('%8d', STIM2.SeqStepList.v));
      if(oldsno ~= STIM2.SeqStepList.v)
         stim_pv(STIM2);
      end;
      
   case 'seq_sequence'
      hcmd = findobj('Tag', 'stmctl_sequence');
      sno = get(hcmd, 'String');
      oldsno = STIM2.SeqStepList.v;
      STIM2.SeqStepList.v = sno;
      set(hcmd, 'String', sprintf('%s', STIM2.SeqStepList.v));
      if(~strcmp(oldsno,STIM2.SeqStepList.v))
         stim_pv(STIM2);
      end;
      
   case 'seq_levflag'
      hcmd = findobj('Tag', 'stmctl_levflag');
      sno = get(hcmd, 'Value');
      if(strcmp(STIM2.LevelFlag.v, 'absolute'))
         sflag  = 1;
      else
         sflag = 0;
      end;
      if(sno == 1)
         STIM2.LevelFlag.v = 'absolute';
      else
         STIM2.LevelFlag.v = 'relative';
      end;
      
      set(hcmd, 'String', sprintf('%s', STIM2.LevelFlag.v));
      if(~strcmp(sflag,sno))
         stim_pv(STIM2);
      end;
      
      return;
      
      
      % button commands:
      
   case 'AmpPlus'
      sign  = 1;
      alpha_edit('updateslider');
      
   case 'AmpMinus'
      sign = -1;
      alpha_edit('updateslider');
      
   case 'close'
      h = findobj('Tag', 'alpha_edit');
      close(h);
      return;
      
   case 'open'
      g2;
      if(isempty(STIM2))
         return;
      end;
      if(~strcmp(STIM2.Method.v, 'pulse'))
         QueMessage('alpha_edit: Require stimulus method PULSE', 1);
         return;
      end;
      h = findobj('Tag', 'alpha-edit');
      if(ishandle(h))
         set(h, 'Name', sprintf('Pulse Editor: %s', STIM2.Name.v));
      end;
      stim_pv(STIM2);
      
   case 'save'
      stim_save(STIM2);       
      
   case 'load' % Put into main stim at DAC0...
      stim_pv(STIM2); % compute
      stim_save(STIM2); % store file
      STIM = STIM2; % update and force display
 %     g(STIM.Name.v);
      struct_edit('load', STIM);
      
      pv('-f'); % force update of main one
      if(SCOPE_FLAG) % if we are in scope mode, stop and restart to update the stimulus.
         acq_stop;
         scope;
      end;
   
   case 'addch' % force to add channel with main stimulus...
      stim_pv(STIM2);
      stim_save(STIM2);
      STIM.Addchannel.v = STIM2.Name.v;
      struct_edit('redisplay', STIM);
      
      pv('-f'); % force update of main one
      if(SCOPE_FLAG) % if we are in scope mode, stop and restart to update the stimulus.
         acq_stop;
         scope;
      end;
      
    case 'superimpose' % force to add channel with main stimulus...
      stim_pv(STIM2);
      stim_save(STIM2);
      STIM.Superimpose.v = STIM2.Name.v;
      struct_edit('redisplay', STIM);
      
      pv('-f'); % force update of main one
      if(SCOPE_FLAG) % if we are in scope mode, stop and restart to update the stimulus.
         acq_stop;
         scope;
      end;

   case 'mouse_down'
      MOUSE_STAT = 1;

      return;
   case 'mouse_up'
      MOUSE_STAT = 0;

      return;
   case 'mouse_moving'
      MOUSE_STAT = 2;
      return;
      
   otherwise
   end;
end;



function [out] = stim_pv(STIM2)

hp = findobj('Tag', 'stimpreview');
if(isempty(hp))
	hp = subplot('Position', [0.05 0.8 0.9 0.12]);
	set(hp, 'color', [0 0 0], ...
      'Tag', 'stimpreview');
end;
subplot(hp);
set(hp, 'color', [0.5 0.5 0.5]); % grey while computing.

STIM2.update = 0;
out = pv(STIM2, 1); % update without showing the mainscreen display

subplot(hp);
%cla(hp);
set(hp, 'color', [0 0 0]);
hold off;
n = length(out.tbase);
for i = 1:n
   plot(out.tbase{i}.v, out.waveform{i}.v); % now display our own copy of the data.
   hold on;
end;
if(strmatch('vsco', fieldnames(out.tbase{1})))
   plot(out.tbase{1}.vsco, out.waveform{1}.vsco, 'color', 'red'); % now display our own copy of the data.
end;
return;


% the following are pulse parameters.

uicontrol('Parent', h, 'Units', 'Normalized', 'Position', [0.05 top 0.15 0.05], ...
   'HorizontalAlignment', 'right', ...
   'Style', 'text', 'String', 'NPulses');
uicontrol('Parent', h, 'units', 'normalized', 'Position', [0.25 top 0.15 0.05], ...
   'HorizontalAlignment', 'right', ...
   'Style', 'edit', 'String', sprintf('%8d', STIM2.Npulses.v), ...
   'Callback', 'alpha_edit(''np'')', ...
   'Tag', 'stmctl_np');

uicontrol('Parent', h, 'Units', 'Normalized', 'Position', [0.05 (top - dy) 0.15 0.05], ...
   'HorizontalAlignment', 'right', ...
   'Style', 'text', 'String', 'Delay (ms)');
uicontrol('Parent', h, 'units', 'normalized', 'Position', [0.25 (top - dy) 0.15 0.05], ...
   'HorizontalAlignment', 'right', ...
   'Style', 'edit', 'String', sprintf('%8d', STIM2.Delay.v), ...
   'Callback', 'alpha_edit(''delay'')', ...
   'Tag', 'stmctl_delay');

uicontrol('Parent', h, 'Units', 'Normalized', 'Position', [0.05 (top - 2 * dy) 0.15 0.05], ...
   'HorizontalAlignment', 'right', ...
   'Style', 'text', 'String', 'IPI (ms)');
uicontrol('Parent', h, 'units', 'normalized', 'Position', [0.25 (top - 2 * dy) 0.15 0.05], ...
   'HorizontalAlignment', 'right', ...
   'Style', 'edit', 'String', sprintf('%8d', STIM2.IPI.v), ...
   'Callback', 'alpha_edit(''ipi'')', ...
   'Tag', 'stmctl_ipi');

uicontrol('Parent', h, 'Units', 'Normalized', 'Position', [0.05 (top - 3 * dy) 0.15 0.05], ...
   'HorizontalAlignment', 'right', ...
   'Style', 'text', 'String', 'Durations');
uicontrol('Parent', h, 'units', 'normalized', 'Position', [0.25 (top - 3 * dy) 0.15 0.05], ...
   'HorizontalAlignment', 'right', ...
   'Style', 'edit', 'String', sprintf('%8d', STIM2.Duration.v), ...
   'Callback', 'alpha_edit(''dur'')', ...
   'Tag', 'stmctl_dur');

uicontrol('Parent', h, 'Units', 'Normalized', 'Position', [0.05 (top - 4 * dy) 0.15 0.05], ...
   'HorizontalAlignment', 'right', ...
   'Style', 'text', 'String', 'Levels');
uicontrol('Parent', h, 'units', 'normalized', 'Position', [0.25 (top - 4 * dy) 0.15 0.05], ...
   'HorizontalAlignment', 'right', ...
   'Style', 'edit', 'String', sprintf('%8d', STIM2.Level.v), ...
   'Callback', 'alpha_edit(''lev'')', ...
   'Tag', 'stmctl_lev');

uicontrol('Parent', h, 'Units', 'Normalized', 'Position', [0.05 (top - 5 * dy) 0.15 0.05], ...
   'HorizontalAlignment', 'right', ...
   'Style', 'text', 'String', 'Sequence');
uicontrol('Parent', h, 'units', 'normalized', 'Position', [0.25 (top - 5 * dy) 0.15 0.05], ...
   'HorizontalAlignment', 'right', ...
   'Style', 'edit', 'String', sprintf('%8d', STIM2.Npulses.v), ...
   'Callback', 'alpha_edit(''np'')', ...
   'Tag', 'stmctl_np');

